package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.business.dto.request.create.VehicleCreateBizDto;
import com.molichuxing.services.exception.VehicleExceptionCodeEnum;
import com.molichuxing.services.infrastructure.bean.CarBean;
import com.molichuxing.services.infrastructure.bean.FenceExceptionBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CarMapper;
import com.molichuxing.services.infrastructure.dao.FenceExceptionBeanMapper;
import com.molichuxing.services.infrastructure.dto.request.create.FenceExceptionCreateDto;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.infrastructure.dto.response.CarModifyDto;
import com.molichuxing.services.infrastructure.dto.response.FenceExceptionDto;
import com.molichuxing.services.infrastructure.service.CarFenceExceptionService;
import com.molichuxing.services.infrastructure.service.CarService;
import com.molichuxing.services.property.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆围栏异常 服务实现类
 * </p>
 *
 * @author
 * @since 2019-08-15
 */
@Service("carFenceExceptionService")
public class CarFenceExceptionServiceImpl implements CarFenceExceptionService {
	private static final Logger logger = LoggerFactory.getLogger(CarFenceExceptionServiceImpl.class);

	@Resource
	private FenceExceptionBeanMapper fenceExceptionBeanMapper;


	@Override
	public Paged<FenceExceptionDto> getFenceExceptionPage(Map<String, Object> map, Integer pageNum, Integer pageSize) {
		logger.info("根据条件查询车辆围栏异常列表。map ： [" + map + "]");
		Paged<FenceExceptionDto> paged = new Paged<>();
		int count = fenceExceptionBeanMapper.getFenceExceptionPageCount(map);
		if (count <= 0) {
			return paged;
		}
		List<FenceExceptionBean> result = fenceExceptionBeanMapper.getFenceExceptionPage(map, pageNum, pageSize);
		logger.info("根据条件查询车辆围栏异常列表 出参：result： " + JSON.toJSONString(result));
		paged.setList(Convert.toFenceExceptionBeanDto(result));
		paged.setTotal(count);
		paged.setPageSize(pageSize);
		return paged;
	}

	@Override
	public FenceExceptionDto getCarsFenceExceptionDetail(Integer fenceExceptionId) {
		return Convert.toFenceExceptionBeanDto(fenceExceptionBeanMapper.selectByPrimaryKey(fenceExceptionId));
	}


	/**
	 * 创建围栏异常信息
	 * @param fenceExceptionCreateDto
	 * @return 主键id
	 */
	@Override
	public Integer createCarFenceException(FenceExceptionCreateDto fenceExceptionCreateDto) {
		//删除该车的围栏异常
		fenceExceptionBeanMapper.delete(Arrays.asList(fenceExceptionCreateDto.getCarId()));
		FenceExceptionBean bean = Convert.toFenceExceptionBean(fenceExceptionCreateDto);
		fenceExceptionBeanMapper.insert(bean);
		return bean.getId();
	}

	@Override
	public Boolean deleteFenceData(List<Integer> carIdList) {
		if((null == carIdList)||(carIdList.size() == 0)) {
			return true;
		}
		int result = fenceExceptionBeanMapper.delete(carIdList);
		if(result > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<Integer> getAllCarId() {
		return fenceExceptionBeanMapper.getAllCarId();
	}
}
